package com.ruoyi.web.controller.system;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Gonggao;
import com.ruoyi.system.service.IGonggaoService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 通知公告Controller
 * 
 * @author zxl
 * @date 2022-12-13
 */
@Controller
@RequestMapping("/system/gonggao")
public class GonggaoController extends BaseController
{
    private String prefix = "system/gonggao";

    @Autowired
    private IGonggaoService gonggaoService;

    @Autowired
    private ISysUserService sysUserService;

    @RequiresPermissions("system:gonggao:view")
    @GetMapping()
    public String gonggao()
    {
        return prefix + "/gonggao";
    }

    /**
     * 查询通知公告列表
     */
    //@RequiresPermissions("system:gonggao:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Gonggao gonggao)
    {
        startPage();
        List<Gonggao> list = gonggaoService.selectGonggaoList(gonggao);
        return getDataTable(list);
    }

    /**
     * 导出通知公告列表
     */
    @RequiresPermissions("system:gonggao:export")
    @Log(title = "通知公告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Gonggao gonggao)
    {
        List<Gonggao> list = gonggaoService.selectGonggaoList(gonggao);
        ExcelUtil<Gonggao> util = new ExcelUtil<Gonggao>(Gonggao.class);
        return util.exportExcel(list, "通知公告数据");
    }

    /**
     * 新增通知公告
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存通知公告
     */
    @RequiresPermissions("system:gonggao:add")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Gonggao gonggao)
    {
        gonggao.setGonggaoid(UUID.randomUUID().toString());
        gonggao.setShifouyuedu(0L);
        gonggao.setIsvalid(1L);

        gonggao.setChuangjianshijian(new Date());

        gonggao.setChuangjianren(getSysUser().getUserName());
        gonggao.setChuangjianrenid(String.valueOf(getUserId()));

        //1:已发布  2:未发布
        if(gonggao.getGonggaozhuangtai().equals("1")){
            gonggao.setFabushijian(new Date());
        }


        return toAjax(gonggaoService.insertGonggao(gonggao));
    }

    /**
     * 修改通知公告
     */
    @RequiresPermissions("system:gonggao:edit")
    @GetMapping("/edit/{gonggaoid}")
    public String edit(@PathVariable("gonggaoid") String gonggaoid, ModelMap mmap)
    {
        Gonggao gonggao = gonggaoService.selectGonggaoByGonggaoid(gonggaoid);
        mmap.put("gonggao", gonggao);
        return prefix + "/edit";
    }

    /**
     * 修改保存通知公告
     */
    @RequiresPermissions("system:gonggao:edit")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Gonggao gonggao)
    {

        //修改未发布变为已发布       1为已发布，2为未发布
        if(gonggao.getFabushijian()==null&&gonggao.getGonggaozhuangtai().equals("1")){
            gonggao.setFabushijian(new Date());
        }

        //修改已发布变为为发布       1为已发布，2为未发布
        if(gonggao.getFabushijian()!=null&&gonggao.getGonggaozhuangtai().equals("2")){
            gonggao.setFabushijian(null);
        }


        return toAjax(gonggaoService.updateGonggao(gonggao));
    }

    /**
     * 删除通知公告
     */
    @RequiresPermissions("system:gonggao:remove")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gonggaoService.deleteGonggaoByGonggaoids(ids));
    }

    @GetMapping("/JieshourenTree")
    public String JieShourenTree()
    {
        return prefix + "/JieshourenTree";
    }

    @PostMapping("/findNewestByLeixing")
    @ResponseBody
    public JsonResult<?>findNewestByLeixing(@RequestParam("gonggaoleixing")String ppGonggaoleixing){
        Gonggao mmGonggao = new Gonggao();
        List<Gonggao> mmList = gonggaoService.findNewestByLeixing(ppGonggaoleixing);
        if (mmList.size()!=0){
            mmGonggao = mmList.get(0);
        }

        return JsonResult.getSuccessResult(mmGonggao);
    }
}
